package com.ooqiu.gaming.ui.main.thread.interact;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.ooqiu.gaming.R;
import com.ooqiu.gaming.api.GamingApi;
import com.ooqiu.gaming.base.BaseFragment;
import com.ooqiu.gaming.common.Constants;
import com.ooqiu.gaming.components.retrofit.progress.ProgressSubscriber;
import com.ooqiu.gaming.components.storage.UserStorage;
import com.ooqiu.gaming.db.Channel;
import com.ooqiu.gaming.ui.main.thread.common.ChannelPageAdapter;
import com.ooqiu.gaming.ui.main.thread.index.content.IndexContentAdapter;
import com.ooqiu.gaming.ui.search.SearchActivity;
import com.ooqiu.gaming.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
@Route(path = InteractFragment.PATH)
public class InteractFragment extends BaseFragment {
    public static final String PATH = "/fragment/interact";
    Unbinder unbinder;
    @BindView(R.id.fake_status_bar)
    View mFakeStatusBar;
    @BindView(R.id.tv_search)
    TextView mTvSearch;
    @BindView(R.id.AppBarLayout01)
    AppBarLayout mAppBarLayout01;
    @BindView(R.id.tab_channel)
    TabLayout mTabChannel;
    @BindView(R.id.page_interact_content)
    ViewPager mPageInteractContent;

    List<Channel> mChannels = new ArrayList<>();

    UserStorage mUserStorage;

    GamingApi mGamingApi;

    BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            LogUtil.e(intent.getAction());
            if (intent.getAction().equals(Constants.ACTION_KEYWORD_CHANGE)) {
                if (mTvSearch != null) {
                    mTvSearch.setText(intent.getBundleExtra("extra").getString("keyword"));
                }
            }
        }
    };


    @Override
    public void initInjector(View view) {
        unbinder = ButterKnife.bind(this, view);
        mUserStorage = getApplicationComponent().getUserStorage();
        mGamingApi = getApplicationComponent().getGamingApi();
    }

    @Override
    public int initContentView() {
        return R.layout.fragment_interact;
    }

    @Override
    public void initUI(View view) {

    }

    @Override
    public void initData() {

    }


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

        mChannels.add(new Channel(null, "0", "0", "全部", Channel.MODULE_ID_INDEX, false, mUserStorage.getToken()));
        mChannels.add(new Channel(null, "1", "1", "未开始", Channel.MODULE_ID_INDEX, false, mUserStorage.getToken()));
        mChannels.add(new Channel(null, "2", "2", "进行中", Channel.MODULE_ID_INDEX, false, mUserStorage.getToken()));
        mChannels.add(new Channel(null, "3", "3", "已结束", Channel.MODULE_ID_INDEX, false, mUserStorage.getToken()));

        ChannelPageAdapter channelPageAdapter = new ChannelPageAdapter(getChildFragmentManager(), mChannels, false) {
            @Override
            public Fragment createFragment(Channel channel) {
                return (BaseFragment) getRouter()
                        .build(InteractContentFragment.PATH)
                        .withInt("statue", Integer.parseInt(channel.getReal_id()))
                        .navigation();
            }
        };


        mPageInteractContent.setAdapter(channelPageAdapter);


        mPageInteractContent.setOffscreenPageLimit(mChannels.size());

        mTabChannel.setupWithViewPager(mPageInteractContent);


        IntentFilter filter = new IntentFilter(Constants.ACTION_INDEX_CATE_CHANGE);
        filter.addAction(Constants.ACTION_LOGIN_STATE_CHANGE);
        filter.addAction(Constants.ACTION_KEYWORD_CHANGE);
        LocalBroadcastManager.getInstance(mContext).registerReceiver(mBroadcastReceiver, filter);
        fetchSearchWordFromNet();
    }

    @Override
    protected View statusView() {
        return null;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.tv_search)
    public void search() {
        getRouter().build(SearchActivity.PATH)
                .withInt("search_type", IndexContentAdapter.ITEM_INTERACT_CONTENT)
                .navigation();
    }


    @Override
    public boolean hasActionBar() {
        return true;
    }


    private void fetchSearchWordFromNet() {
        mGamingApi.searchKeyWord(1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<String>>(mContext) {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onNext(List<String> strings) {
                        if (!strings.isEmpty()) {
                            mTvSearch.setText(strings.get(0));
                        }
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }
}
